import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $: any;
@Component({
	selector: 'sa-app-version-deploy',
	templateUrl: './app-version-deploy.component.html',
	styleUrls: ['./app-version-deploy.component.css']
})
export class AppVersionDeployComponent implements OnInit {
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	activeSelected: string = ''; //row高亮
	// 查询参数
	searchKey:any=""; // 用户的搜索关键字

	// 标志是否选择车辆的
	isShowDelete: any = false; //是否有删除权限
	// 是编辑还是新增的
	formType: any = 'add';
	// 是否启用小程序的参数
	isCheck: any = true;
	type: any= ''; //app类型 1安卓 2IOS
	isTips: any = ''; //是否需要提示更新
	isCheckTips: any = true;
	isForce: any = ''; // 是否强制更新
	isCheckForce: any = true;
	// 表单验证
	validatorOptions = {
		excluded: [':disabled'],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			appName: {
				validators: {
					notEmpty: {
						message: '请填写app名称'
					},
				}
			},
			version: {
				validators: {
					notEmpty: {
						message: '请填写app版本'
					},
				}
			},
			content: {
				validators: {
					notEmpty: {
						message: '请填写更新内容'
					},
				}
			},
		}
	}
	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:AppVersionDeploy')));
		if (!btnData) {
			return
		};
		for (let item of btnData) {
			if (item.sourceKey === "system:AppVersionDeploy:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}

		}
	}
	load() {
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				searchKey:this.searchKey,
				type: this.type,
				isTips: this.isTips,
				isForce: this.isForce
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("applets/getAppVersionList.html", reqdata).subscribe(result => {
			let data = []
			if (result.isSuccess && result.exhibitDatas) {
				data = result.exhibitDatas
				this.tableData = result.exhibitDatas
				this.totalCounts = result.recordCount;
			}
		});
	}
	// 分页回调
	paginate(e) {
		this.activeSelected = '';
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 刷新表
	refresh() {
		this.load();
	}
	// 删除续费记录
	del_domCode(row) {
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("applets/delAppVersion.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 表单查询
	tableSearch() {
		this.curPage = 1;
		this.load();
	}


	isScroTopNum: any = 0;  //当用户重新点开添加再进行点击车辆选择时才滚动到头部 其他时候不滚动
	// 限制进厂的配置提交对象
	appVersionDeployObj: any = {
		appName: "",
		version: '',
		type: '',
		content: '',
		isTips: '',
		isForce: '',
	};
	// 

	// 添加续费记录的弹窗
	showAddLimitModal(modal) {
		this.formType = 'add'
		this.isScroTopNum = 0;
		modal.show();
		this.resetForm();
	}
	// 重置添加表
	resetForm() {
		this.appVersionDeployObj = {
			appName: "",
			version: '',
			type: '1',
			content: '',
			isTips: '1',
			isForce: '1',
		};
		this.isCheck = true;
		this.isCheckTips = true;
		this.isCheckForce = true;
		//初始化表单
		$("#appVersionDeploy").data('bootstrapValidator').resetForm();
	}
	// 关闭添加弹框
	closeappVersionDeployModal(modal) {
		$("#appVersionDeploy").data('bootstrapValidator').resetForm();
		modal.hide();
	}

	clickCheck(value){
		this.appVersionDeployObj.type = value
		if (value == '1') {
			this.isCheck = true
		} else {
			this.isCheck = false
		}
	}
	clickisTips(value){
		this.appVersionDeployObj.isTips = value
		if (value == '1') {
			this.isCheckTips = true
		} else {
			this.isCheckTips = false
		}
	}
	clickForce(value){
		this.appVersionDeployObj.isForce = value
		if (value == '1') {
			this.isCheckForce = true
		} else {
			this.isCheckForce = false
		}
	}
	// 提交添加结果
	submitModal(modal) {
		$('#appVersionDeploy').bootstrapValidator('validate'); //开启验证
		let isValid = $('#appVersionDeploy').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.appVersionDeployObj.type = this.isCheck ? 1 : 2;
			this.appVersionDeployObj.isTips = this.isCheckTips ? 1 : 0;
			this.appVersionDeployObj.isForce = this.isCheckForce ? 1 : 0;
			let reqdata = this.appVersionDeployObj
			this.jsonApiService.http_obj("applets/addAppVersion.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					// 
					this.load();
					this.closeappVersionDeployModal(modal)
				}
			})
		}
	}
	// 编辑
	editRow(row, modal) {
		modal.show();
		setTimeout(() => {
			this.formType = 'edit'
			this.appVersionDeployObj = JSON.parse(JSON.stringify(row))
			this.isCheck = this.appVersionDeployObj.type == 1? true : false;
			this.isCheckTips = this.appVersionDeployObj.isTips? true : false;
			this.isCheckForce = this.appVersionDeployObj.isForce? true : false;
		}, 300);
	}
}